<!DOCTYPE html>
<html>
<head>   
    <title>profiles</title>  
    <script type="text/javascript" src="../javascript/page/Child.js"></script>
    <style type="text/css">
        html, body {
            font-size: 12px;  background-color: #f1f1f1;
        }
    </style>
</head>
<body onload="docLoaded();">
    <div id="queryForm" class="title_bar">
        <template>
            <div class="title_bar">
                <el-page-header @back="returnBack" :content="appName"></el-page-header>
            </div>  
            <div class="query_form">
                <el-form :inline="true" :model="searchData" class="demo-form-inline" size="small">
                    <el-form-item label="Profile">
                        <el-radio-group v-model="searchData.profile" @change="handleQueryEvent">    
                            <el-radio-button v-for="(item,i) in profileList" :key="i" :label="''+item.val">{{item.name}}</el-radio-button>                         
                        </el-radio-group>
                    </el-form-item>                   
                    <el-form-item label="Label">
                        <el-select v-model="searchData.label" placeholder="please select label" @change="handleQueryEvent">
                            <el-option v-for="item in labelList" :key="item.val" :label="item.name" :value="item.val"></el-option>
                        </el-select>
                    </el-form-item>   
                    <el-form-item label="Property">
                        <el-input v-model="searchData.propertyKey" placeholder="Property name"></el-input>
                    </el-form-item>        
                    <el-form-item>
                        <el-button type="primary" @click="onSubmit">Query</el-button>
                    </el-form-item>
                    <el-form-item>
                        <el-button type="primary" @click="handleAdd" icon="el-icon-plus" plain>Create</el-button>
                    </el-form-item>
                    <el-form-item>
                        <el-button type="primary" @click="handleView" icon="el-icon-view" plain>View</el-button>                       
                    </el-form-item>                  
                    <el-form-item>
                        <el-button type="primary" @click="handleCopy" icon="el-icon-document-copy" plain>Clone</el-button>
                    </el-form-item>
                   
                </el-form>
            </div>       
            <div class="hline"></div>
            <div class="panel">              
                <el-table :data="tableData" style="width: 99%" max-height="660">
                    <el-table-column prop="id" label="ID" min-width="5%"></el-table-column>
                    <el-table-column prop="profile" label="Profile" min-width="5%"></el-table-column>                    
                    <el-table-column prop="propertyKey" label="Property" min-width="25%"></el-table-column>
                    <el-table-column prop="propertyValue" label="Value" min-width="30%"></el-table-column>
                    <el-table-column prop="label" label="Label" min-width="10%"></el-table-column>
                    <el-table-column label="Status" min-width="5%">
                        <template slot-scope="scope">
                            <el-tag :type="scope.row.statusType">{{scope.row.statusName}}</el-tag>
                        </template>
                    </el-table-column>
                    <el-table-column label="Operation" min-width="19%">
                        <template slot-scope="scope">                           
                            <el-button @click="handleEdit(scope.row)" type="text" size="small">编辑</el-button>
                        </template>
                    </el-table-column>
                </el-table>                 
            </div>   
            <div class="status_bar" style="text-align: left;">
                <el-pagination
                    @size-change="handleSizeChange"
                    @current-change="handlePageChange"
                    :current-page="searchData.pageNum"
                    :page-sizes="[8, 15, 20]"
                    :page-size="searchData.pageSize"
                    layout="total, sizes, prev, pager, next, jumper"
                    :total="pageInfo.total">
                </el-pagination> 
            </div>
            <el-dialog :title="dialogTitle" :visible.sync="editDialogFormVisible"> 
                <el-form :model="dialogForm"  :label-width="formLabelWidth" :rules="dialogFormRules" ref="dialogForm">  
                    <el-row>
                        <el-col span="12">
                            <el-form-item label="Profile">
                                <el-input v-model="dialogForm.profile" disabled></el-input>
                            </el-form-item>
                        </el-col>
                        <el-col span="12">
                            <el-form-item label="Label">
                                <el-input v-model="dialogForm.label" disabled></el-input>
                            </el-form-item>    
                        </el-col>
                    </el-row>
                    <el-row>
                        <el-col  span="24">
                            <el-form-item label="Property" prop="propertyKey">
                                <el-input v-model="dialogForm.propertyKey" autocomplete="off"></el-input>
                            </el-form-item>
                        </el-col>
                    </el-row>
                    <el-row>
                        <el-col  span="24">
                            <el-form-item label="Value">
                                <el-input v-model="dialogForm.propertyValue" autocomplete="off" type="textarea"></el-input>
                            </el-form-item>
                        </el-col>
                    </el-row>
                    <el-row>
                        <el-col span="12">
                            <el-form-item label="ValueType" size="small">
                                <el-select v-model="dialogForm.valueType" placeholder="please select status">
                                    <el-option v-for="item in valueTypeList"
                                    :key="item.vtype"
                                    :label="item.name"
                                    :value="item.vtype">
                                    </el-option>
                                </el-select>
                            </el-form-item>           
                        </el-col>
                        <el-col span="12">
                            <el-form-item label="Status">
                                <el-select v-model="dialogForm.status" placeholder="please select status">
                                    <el-option v-for="item in statusList"
                                    :key="item.status"
                                    :label="item.name"
                                    :value="item.status">
                                    </el-option>
                                </el-select>
                            </el-form-item>
                        </el-col>
                    </el-row>                   
                    <el-row>
                        <el-col span="24">
                            <el-form-item label="Remark"  size="small">
                                <el-input v-model="dialogForm.remark" autocomplete="off" type="textarea"></el-input>
                            </el-form-item>
                        </el-col>
                    </el-row>    
                </el-form>
                <div slot="footer" class="dialog-footer">
                    <el-button @click="editDialogFormVisible = false">取 消</el-button>
                    <el-button type="primary" @click="editSubmit">确 定</el-button>
                </div>                
            </el-dialog>
            <el-dialog title="CopyTo" :visible.sync="copyDialogFormVisible"> 
                <el-form :model="copyDialogForm">  
                    <el-form-item label="From Profile" :label-width="formLabelWidth">
                        <el-input v-model="copyDialogForm.profile" disabled></el-input>
                    </el-form-item>
                    <el-form-item label="From Label" :label-width="formLabelWidth">
                        <el-input v-model="copyDialogForm.label" disabled></el-input>
                    </el-form-item>
                    <el-form-item label="Target Profile" :label-width="formLabelWidth">
                        <el-select v-model="copyDialogForm.targetProfile" placeholder="please select profile">
                            <el-option v-for="item in profileList"
                            :key="item.val"
                            :label="item.name"
                            :value="item.val">
                            </el-option>
                        </el-select>
                    </el-form-item>    
                    <el-form-item label="Target Label" :label-width="formLabelWidth">                        
                        <el-select v-model="copyDialogForm.targetLabel" placeholder="please select label">
                            <el-option v-for="item in labelList"
                            :key="item.val"
                            :label="item.name"
                            :value="item.val">
                            </el-option>
                        </el-select>
                    </el-form-item>    
                </el-form>
                <div slot="footer" class="dialog-footer">
                    <el-button @click="copyDialogFormVisible = false">取 消</el-button>
                    <el-button type="primary" @click="copySubmit">确 定</el-button>
                </div>  
            </el-dialog>                      
        </template>
    </div>
    <script type="text/javascript">
        var router = startPage();        
        function docLoaded() {
            var applicationId = getURLParams("appId");
            var applicationName = getURLParams("appName");
            var queryApp = new Vue({el: '#queryForm', data: {
                    appName: applicationName, 
                    searchData:{
                        appId: applicationId,                  
                        profile:"dev",
                        label:"master",    
                        propertyKey:"",
                        pageNum:1,
                        pageSize:8                       
                    },
                    pageInfo:{
                        totalPage:0,
                        total:0
                    },
                    tableData:[],                
                    profileList:[{val:"dev",name:"DEV"},{val:"sit",name:"SIT"},{val:"prod",name:"PROD"}],               
                    labelList:[{val:"master",name:"master"},{val:"local",name:"local"}],                   
                    statusList:[{status:0,name:"normal"},{status:1,name:"disable"}],
                    valueTypeList:[{vtype:0,name:"default"},{vtype:1,name:"password"}],
                    formLabelWidth: '120px',
                    editDialogFormVisible:false,                    
                    dialogTitle: "Edit",
                    dialogMode: 0,
                    dialogForm:{                      
                        id:0,
                        appId:applicationId,
                        application:applicationName, 
                        profile: "", 
                        label:"master", 
                        propertyKey:"", 
                        propertyValue:"", 
                        valueType:0,
                        remark:"", 
                        status: 0
                    },
                    copyDialogFormVisible:false,
                    copyDialogForm:{   
                        appId:applicationId,                      
                        profile: "dev", 
                        label:"master", 
                        targetProfile:"dev",
                        targetLabel:"master"
                    },
                    dialogFormRules: {                       
                        propertyKey: [
                            { required: true, message: '请输入属性名称', trigger: 'blur' },
                            { min: 1, max: 128, message: '长度在 1 到 128 个字符', trigger: 'blur' }
                        ]                       
                    }   
                },
                methods: {
                    returnBack() { window.history.back(-1); },
                    onSubmit:function(event){                                                          
                        httpPost(apiConfigProfiles(this,router.authInfo,JSON.stringify(this.searchData))).success(function(res,context){ 
                            parseAndCheckResult(context,res);
                        }).then(function(status, res, context){context.$message.error(res);});
                    },
                    handleSizeChange(val) {                     
                        this.searchData.pageSize = val;
                        this.onSubmit();
                    },
                    handlePageChange(val) {
                        this.searchData.pageNum = val;
                        this.onSubmit();
                    },
                    handleQueryEvent(val){                       
                        this.onSubmit();
                    },
                    handleEdit(row){
                        this.dialogForm = {
                            id:row.id, 
                            appId:row.appId, 
                            application:row.application, 
                            profile:row.profile, 
                            label:row.label, 
                            propertyKey:row.propertyKey,
                            propertyValue:row.propertyValue, 
                            valueType:row.valueType,
                            remark:row.remark, 
                            status:row.status
                        };                       
                        this.dialogTitle = "Update Property";
                        this.dialogMode = 0;
                        this.editDialogFormVisible = true;
                    },                   
                    handleAdd(){
                        this.dialogForm = {
                            id:0, 
                            appId:applicationId, 
                            application:applicationName, 
                            profile: this.searchData.profile, 
                            label:this.searchData.label, 
                            propertyKey:"",
                            propertyValue:"", 
                            valueType:0,
                            remark:"", 
                            status:0
                        };                           
                        this.dialogTitle = "Create New Property";
                        this.dialogMode = 1;
                        this.editDialogFormVisible = true;
                    },
                    editSubmit:function(event){
                        this.editDialogFormVisible = false;
                        var apiRequest = (this.dialogMode)?apiConfigProfileAdd(this,router.authInfo,JSON.stringify(this.dialogForm)):apiConfigProfileUpdate(this,router.authInfo,JSON.stringify(this.dialogForm));
                        httpPost(apiRequest).success(function(res,context){                          
                            checkResult(context,res,false);
                            context.searchData.label = context.dialogForm.label;
                            context.onSubmit();
                        }).then(function(status, res, context){checkResult(context,res,true);});
                    },                   
                    handleCopy:function(event){                       
                        this.copyDialogForm.profile = this.searchData.profile;     
                        this.copyDialogForm.label = this.searchData.label;   
                        this.copyDialogForm.targetLabel = "";   
                        this.copyDialogForm.targetProfile = "";  
                        this.copyDialogFormVisible = true;       
                    },
                    copySubmit:function(event){
                        var apiRequest = apiConfigProfileSync(this,router.authInfo,JSON.stringify(this.copyDialogForm));   
                        this.copyDialogFormVisible = false; 
                        httpPost(apiRequest).success(function(res,context){                          
                            checkResult(context,res,false);
                        }).then(function(status, res, context){checkResult(context,res,true);});             
                    },
                    handleView:function(event){
                        var viewUrl = '/auth/datapages/profiles_edit.htm?appId='+this.searchData.appId+'&appName='+this.appName+'&profile='+this.searchData.profile+'&label='+this.searchData.label;
                        var title = this.appName+" ("+this.searchData.profile+" , "+this.searchData.label+")";
                        openDialogPage(viewUrl, title, 950, 550);
                    }
                }
            }); queryApp.onSubmit();//init end.
        } 

        function parseAndCheckResult(vApp, res){
            var resObj = JSON.parse(res); 
            if(resObj.code != SUCCESS_CODE){
                return false;
            }
            if(resObj.pageInfo){
                vApp.pageInfo.total = resObj.pageInfo.total;
                vApp.pageInfo.totalPage = resObj.pageInfo.totalPage;
            }
            if(resObj.data){                
                var arrayObj = new Array();
                for(var item of resObj.data){
                    arrayObj.push({
                        id:item.id,
                        appId:item.appId,
                        application:item.application, 
                        profile:item.profile, 
                        label:item.label, 
                        propertyKey:item.propertyKey, 
                        propertyValue:item.propertyValue, 
                        valueType:item.valueType,
                        remark:item.remark, 
                        status:item.deleted,
                        statusName:(item.deleted==1)?"disable":"normal",
                        statusType:(item.deleted==1)?"info":"success"});
                }
                vApp.tableData = arrayObj;
            }
            return resObj;
        }

        function checkResult(vApp, res, isErr){            
            if(isErr){
                vApp.$message.error(res);
                return 0;
            }
            var resObj = JSON.parse(res); 
            if(resObj.code == SUCCESS_CODE){
                vApp.$message({type: 'success', message: resObj.message});
                return 1;
            }
            vApp.$message.error(resObj.message+","+resObj.code);
            return 0;
        }
    </script>
</body>
</html>
